package edu.bbk.muc.lifetracker.apitest;

import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;
import edu.bbk.muc.lifetracker.LifeTracker;
import edu.bbk.muc.lifetracker.common.Acceleration;
import edu.bbk.muc.lifetracker.common.SensorsToUi;
import edu.bbk.muc.lifetracker.common.SimpleLocation;

/**
 * Test for our {@link SensorsMockThread} - we ensure, that it actually provides
 * correct mock data
 */
public class SensorsMockThreadTest extends ActivityInstrumentationTestCase2<LifeTracker> {
  private final class MockSensors implements SensorsToUi {
    float[] lightValues = new float[] { 160f, 170f, 180f, 200f };
    int lightCounter = 0;

    @Override
    public void onAccelerationChanged(long timestamp, Acceleration acceleration) {
      Acceleration expectedAcc = new Acceleration(1, 2, 3);
      assertEquals(expectedAcc, acceleration);
    }

    @Override
    public void onAmbientLightChanged(long timestamp, float light) {
      Log.d("Light", "Light = " + light);
      assertEquals(lightValues[lightCounter++], light);
    }

    @Override
    public void onLocationChanged(long timestamp, SimpleLocation location) {
    }
  }

  private long timestampStart = -1;
  private final long TIMEOUT = 3000;

  public SensorsMockThreadTest() {
    super(LifeTracker.class);
  }

  public void testSensorsInterface() throws InterruptedException {

    MockSensors mockSensors = new MockSensors();
    SensorsMockThread mockSensorsRunnable = new SensorsMockThread(mockSensors);

    Thread t = new Thread(mockSensorsRunnable);
    t.run();

    t.join();

    assertEquals(4, mockSensors.lightCounter);
  }
}
